<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Permission\Traits\HasRoles;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable, HasRoles, SoftDeletes;

    /**
     * 与模型关联的数据表
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
//    protected $fillable = [
//        'name', 'email',
//    ];

    /**
     * 主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 需要被转换成日期的属性。
     *
     * @var array
     */
    protected $dates = ['createTime', 'updateTime', 'startTime', 'endTime'];

    /**
     * @var array
     */
    protected $guarded = ['id'];

    /**
     * @var bool
     */
    public $timestamps = false;

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password'
    ];

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * 用户登录记录
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function loginLog(){
        return $this->hasMany(UserLoginLog::class, 'userId', 'id');
    }

    /**
     * 用户具有的角色
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function role(){
        return $this->hasMany(ModelsRoles::class, 'model_id', 'id');
    }

    /**
     * 获取用户总余额
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function totalAmount(){
        return $this->hasOne(UserAmountSummary::class, 'userId', 'id');
    }

    /**
     * 获取用户总积分
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function totalIntegral(){
        return $this->hasOne(UserIntegralSummary::class, 'userId', 'id');
    }
    /**
     * 下级
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function fanNum(){
        return $this->hasMany(self::class, 'parentId', 'id');
    }

    /**
     * 实名认证信息
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function authentication(){
        return $this->hasOne(UserAuthentication::class, 'userId', 'id');
    }
}
